//
//  DDateAnalysisChartFooterView.swift
//  Drop
//
//  Created by hushaohua on 2018/4/6.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DDateAnalysisChartFooterView: UITableViewHeaderFooterView {

    static let pieReuseId:String = "PIE";
    static let barReuseId:String = "BAR";
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier);
        if (reuseIdentifier!.contains(DDateAnalysisChartFooterView.pieReuseId)){
            self.contentView.addSubview(self.pieChartView);
        }else if (reuseIdentifier!.contains(DDateAnalysisChartFooterView.barReuseId)){
            self.contentView.addSubview(self.scrollView);
            self.scrollView.addSubview(self.barChartView);
        }
        self.contentView.backgroundColor = UIColor.white;
    }
    
    lazy var pieChartView:HPieChartView = {
        let width = UIScreen.main.bounds.width - 30;
        var chartView:HPieChartView = HPieChartView(frame:CGRect(x:15,y:10,width:width,height:100));
        chartView.backgroundColor = UIColor.white;
        return chartView;
    }();
    
    lazy var scrollView:UIScrollView = {
        let width = UIScreen.main.bounds.width;
        var scrollView:UIScrollView = UIScrollView(frame: CGRect(x:0,y:10,width:width,height:80));
        scrollView.showsHorizontalScrollIndicator = false;
        return scrollView;
    }();
    
    lazy var barChartView:HBarChartView = {
        let width = UIScreen.main.bounds.width - 30;
        var chartView:HBarChartView = HBarChartView(frame:CGRect(x:15,y:0,width:width,height:80));
        chartView.barConfig().valueAxisPref?.textWidth = 24;
        let pref = HChartAxisPreference();
        pref.textColor = UIColor.darkGray;
        chartView.barConfig().barColor = UIColor(white:0.5, alpha:0.8);
        chartView.barConfig().valuePref = pref;
        chartView.barConfig().barWidth = 30;
        chartView.barConfig().barSpace = 10;
        chartView.backgroundColor = UIColor.white;
        return chartView;
    }();
    
    func setBarCharts(_ charts:[HBarChartItem]){
        self.barChartView.chartItems = charts;
        var maxWidth = self.barChartView.horizontalWidth;
        if (maxWidth < UIScreen.main.bounds.width - 30){
            maxWidth = UIScreen.main.bounds.width - 30;
        }
        self.scrollView.contentSize.width = maxWidth + 30;
        self.barChartView.frame.size.width = maxWidth;
    }
    func setPieCharts(_ charts:[HPieChartItem]){
        self.pieChartView.chartItems = charts;
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
